package com.naiterui.ehp.bs.weixin.controller;

import cn.hutool.json.JSONUtil;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.weixin.exception.WeixinCode;
import com.naiterui.ehp.bs.weixin.exception.WeixinException;
import com.naiterui.ehp.bs.weixin.service.UserService;
import com.naiterui.ehp.bs.weixin.vo.LoginUserVO;
import com.naiterui.ehp.bs.weixin.vo.LoginVO;
import com.naiterui.ehp.bs.weixin.vo.MiniappPhoneParamVO;
import com.naiterui.ehp.bs.weixin.vo.MiniappPhoneVO;
import com.naiterui.ehp.bs.weixin.vo.RegisterParamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

@Slf4j
@RequestMapping("login")
@RestController
@RequiredArgsConstructor
@Api(value = "login", tags = SwaggerTag.WEIXIN + SwaggerTag.USER_ACCOUNT)
public class LoginController {

    private final UserService userService;

    @ApiOperation("小程序登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "微信小程序登录code", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "scene", value = "扫码场景参数", dataType = "string", paramType = "query") })
    @GetMapping(value = "miniapp/{code}")
    public LoginVO miniappLogin(@PathVariable @NotNull(message = "登录code不能为空") String code, @Nullable String scene)
            throws BusinessException {
        log.info("miniappLogin：code={},scene={}", code, scene);
        LoginVO loginVO;
        try {
            loginVO = userService.miniappLogin(code, scene);
        } catch (WxErrorException e) {
            log.info("小程序登录异常， 异常信息 {}", e);
            throw new WeixinException(WeixinCode.WECHAT_LOGIN_FAIL);
        }
        return loginVO;
    }

    @ApiOperation("小程序注册绑定")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "registerParamVO", value = "注册绑定参数", required = true, dataType = "RegisterParamVO", dataTypeClass = RegisterParamVO.class, paramType = "body") })
    @PostMapping("miniapp")
    public LoginUserVO miniappRegister(@RequestBody RegisterParamVO registerParamVO) throws WeixinException {
        log.info("miniappRegister，registerParamVO:{}", JSONUtil.toJsonStr(registerParamVO));
        LoginUserVO loginUserVO = userService.miniappRegister(registerParamVO);
        if (loginUserVO == null) {
            throw new WeixinException(WeixinCode.PATIENT_NOT_EXIST);
        }
        return loginUserVO;
    }

    @ApiOperation("小程序获取授权手机号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "registerParamVO", value = "注册绑定参数", required = true, dataType = "RegisterParamVO", dataTypeClass = RegisterParamVO.class, paramType = "body") })
    @PostMapping("miniapp/phone")
    public MiniappPhoneVO miniappPhone(@RequestBody MiniappPhoneParamVO miniappPhoneParamVO) throws WeixinException {
        log.info("miniappPhone，miniappPhoneParamVO:{}", JSONUtil.toJsonStr(miniappPhoneParamVO));
        MiniappPhoneVO miniappPhone = userService.miniappPhone(miniappPhoneParamVO);
        return miniappPhone;
    }

}
